package com.ruibang.glass.iso.component;

import com.ruibang.glass.common.constants.Constants;
import com.ruibang.glass.iso.entity.PermissionManage;
import com.ruibang.glass.iso.service.impl.PermissionManageService;
import com.teaming.cloud.framework2.elastic.job3.annotation.ElasticJobScheduler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: songJinKang
 * @CreateTime: 2024-06-18  15:44
 * @Description: TODO:权限管理任务
 * @Version: 1.0
 */
@Slf4j
@Component
//@ElasticJobScheduler(cron = "0 5 0 * * ?")
@ElasticJobScheduler(cron = "0 * * * * ?")
public class PermissionManageTask implements SimpleJob {

    @Autowired
    private PermissionManageService permissionManageService;

    @Override
    public void execute(ShardingContext shardingContext) {
        //每天定时刷新权限状态
        List<PermissionManage> permissionManageList = permissionManageService.list();
        if (CollectionUtils.isNotEmpty(permissionManageList)) {
            LocalDate currentDate = LocalDate.now();
            List<String> updateData = new ArrayList<>();
            for (PermissionManage permissionManage : permissionManageList) {
                LocalDate checkEndDate = permissionManage.getCheckEndDate();
                LocalDate editEndDate = permissionManage.getEditEndDate();
                String status = permissionManage.getStatus();
                if ((checkEndDate != null && currentDate.compareTo(checkEndDate) > 0) && (editEndDate != null && currentDate.compareTo(editEndDate) > 0) && status.equals(Constants.ONE_STR)) {
                    updateData.add(permissionManage.getId());
                }
            }
            //修改状态
            if (CollectionUtils.isNotEmpty(updateData)){
                permissionManageService.editStatusById(updateData,Constants.ZERO_STR);
            }
        }
    }

}
